package org.wk.core.db.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;

import org.springframework.util.StringUtils;
import org.wk.core.db.dao.lang.covert.BooleanToString;
import org.wk.core.db.tool.WebUtils;


/**
 * 
 * @ClassName	: BaseEntity
 * @Package		：org.wk.core.db.domain
 * @Author df.w 
 * @Create Time	：2018年5月2日 上午9:54:29
 * @Description	:
 */
@MappedSuperclass
public class BaseEntity implements Serializable{
	private static final long serialVersionUID = 1L;
	/**
	 * 状态
	 */
	@Column(name = "status")
	private String status;
	/**
	 * 创建者
	 */
	@Column(name = "creator",updatable=false)
	private String creator;
	/**
	 * 创建时间
	 */
	@Column(name = "create_time",updatable=false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;
	/**
	 * 修改者
	 */
	@Column(name = "modifier")
	private String modifier;
	/**
	 * 修改时间
	 */
	@Column(name = "modify_time")
	@Temporal(TemporalType.TIMESTAMP)
	private Date modifyTime;
	/**
	 * 版本控制
	 */
	@Version
	private Integer version;
	/**
	 * 删除标志
	 */
	@Column(name = "del_flag")
	private String delFlag;
	
	/**
	 * 数据为只读
	 */
	@Column(name = "read_only")
	@Convert(converter=BooleanToString.class)
	private Boolean readOnly=false;

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	public Boolean getReadOnly() {
		return readOnly;
	}

	public void setReadOnly(Boolean readOnly) {
		this.readOnly = readOnly;
	}
	@PrePersist
	public void persist() {
		IUser userDetail = WebUtils.getUser();
		if(userDetail!=null){
			String userId = userDetail.getId();
			if(!StringUtils.isEmpty(userId)){
				this.setCreator(userId);
				this.setModifier(userId);
			}
		}
		this.setCreateTime(new Date());
		this.setModifyTime(new Date());
	}
	@PreUpdate
	public void update() {
		IUser userDetail = WebUtils.getUser();
		if(userDetail!=null){
			String userId = userDetail.getId();
			if(!StringUtils.isEmpty(userId)){
				this.setModifier(userId);
			}
		}
		this.setModifyTime(new Date());
	}
}
